package cn.iocoder.yudao.module.book.controller.admin.methods;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.methods.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.methods.MethodsDO;
import cn.iocoder.yudao.module.book.service.methods.MethodsService;

@Tag(name = "管理后台 - 启蒙方法")
@RestController
@RequestMapping("/book/methods")
@Validated
public class MethodsController {

    @Resource
    private MethodsService methodsService;

    @PostMapping("/create")
    @Operation(summary = "创建启蒙方法")
    @PreAuthorize("@ss.hasPermission('book:methods:create')")
    public CommonResult<Integer> createMethods(@Valid @RequestBody MethodsSaveReqVO createReqVO) {
        return success(methodsService.createMethods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新启蒙方法")
    @PreAuthorize("@ss.hasPermission('book:methods:update')")
    public CommonResult<Boolean> updateMethods(@Valid @RequestBody MethodsSaveReqVO updateReqVO) {
        methodsService.updateMethods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除启蒙方法")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:methods:delete')")
    public CommonResult<Boolean> deleteMethods(@RequestParam("id") Integer id) {
        methodsService.deleteMethods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得启蒙方法")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:methods:query')")
    public CommonResult<MethodsRespVO> getMethods(@RequestParam("id") Integer id) {
        MethodsDO methods = methodsService.getMethods(id);
        return success(BeanUtils.toBean(methods, MethodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得启蒙方法分页")
    @PreAuthorize("@ss.hasPermission('book:methods:query')")
    public CommonResult<PageResult<MethodsRespVO>> getMethodsPage(@Valid MethodsPageReqVO pageReqVO) {
        PageResult<MethodsDO> pageResult = methodsService.getMethodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MethodsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出启蒙方法 Excel")
    @PreAuthorize("@ss.hasPermission('book:methods:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMethodsExcel(@Valid MethodsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MethodsDO> list = methodsService.getMethodsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "启蒙方法.xls", "数据", MethodsRespVO.class,
                        BeanUtils.toBean(list, MethodsRespVO.class));
    }

}